﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QWeiBo.ViewModel;
using TencentWeiboSDK.Services;
using TencentWeiboSDK.Services.Util;
using System.Threading;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using TencentWeiboSDK.Model;

namespace QWeiBoN.ViewModule
{
    /// <summary>
    /// 微博转播类
    /// </summary>
    public class RepostTextModel : BaseViewModel
    {

        /// <summary>
        /// 实例化服务类
        /// </summary>
        WeiboService service = new WeiboService();
        // 如果需要图片
        //private PhotoChooserTask photoTask = new PhotoChooserTask();
        //private string text = string.Empty;
        //private BitmapImage imageSource;

        /// <summary>
        /// 用于转发微博
        /// </summary>
        /// <param name="uId"></param>
        /// <param name="textId"></param>
        /// <param name="action"></param>
        public void repostUserText(string textId, string text, BitmapImage imageSource, Action<Callback<Object>> action)
        {

            //若用户选择了图片，则实例化 UploadPic 对象，用于上传图片
            //注意：必须在UI线程实例化该对象！
            UploadPic pic = (null != imageSource) ? new UploadPic(imageSource) : null;

            // 为了增加用户体验，进行异步操作
            new Thread(() =>
            {
                //Action<Callback<object>> action1 = new Action<Callback<object>>((callBack) =>
                //{
                //    Deployment.Current.Dispatcher.BeginInvoke(() =>
                //    {
                //        if (null == callBack.InnerException)
                //        {
                //            MessageBox.Show("微博转播成功");
                //        }
                //        else
                //        {
                //            MessageBox.Show(callBack.InnerException.Message);
                //        }
                //    });

                //    if (null != action)
                //    {
                //        action(callBack);
                //    }
                //});

                // 不带图片的转发
                if (text.Trim() == string.Empty)
                {
                    if (null == imageSource)
                    {
                        service.Repost(new ServiceArgument() { Reid = textId }, null);
                    }
                    else
                    {
                        service.Repost(new ServiceArgument() { Reid = textId, Pic = pic }, null);
                    }
                }
                else
                {
                    if (null == imageSource)
                    {
                        service.Repost(new ServiceArgument() { Reid = textId, Content = text }, null);
                    }
                    else
                    {
                        service.Repost(new ServiceArgument() { Reid = textId, Content = text, Pic = pic }, null);
                    }
                }
                // 切换并回调到UI线程.
                Deployment.Current.Dispatcher.BeginInvoke(() =>
                {
                    if (null != action)
                    {
                        action(null);
                    }
                });

            }).Start();
        }
    }
}
